package com.example.growthmuseum.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.growthmuseum.AppController;
import com.example.growthmuseum.MainActivity;
import com.example.growthmuseum.R;
import com.example.growthmuseum.ServiceFactory;
import com.example.growthmuseum.data.model.Result;
import com.example.growthmuseum.data.model.User;
import com.example.growthmuseum.service.UserService;
import com.orhanobut.logger.Logger;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Roman on 2016/10/30.
 */
public class LoginActivity extends AppCompatActivity
        implements View.OnClickListener {

    private EditText usernameEdit;

    private EditText pwdEdit;

    private Button loginBtn;

    private Button registerBtn;

    private UserService userService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initViews();
        this.userService = ServiceFactory
                .createRetrofitService(UserService.class,
                        ServiceFactory.BASE_URL);
    }

    private void initViews() {
        usernameEdit = (EditText) findViewById(R.id.username_edit);
        pwdEdit = (EditText) findViewById(R.id.pwd_edit);
        loginBtn = (Button) findViewById(R.id.login_btn);
        loginBtn.setOnClickListener(this);
        registerBtn = (Button) findViewById(R.id.register_btn);
        registerBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_btn:
                if (usernameEdit.getText().toString().trim().isEmpty()) {
                    Toast.makeText(LoginActivity.this,
                            getResources().getString(R.string.user_name_cannot_empty),
                            Toast.LENGTH_SHORT).show();
                } else if (pwdEdit.getText().toString().trim().isEmpty()) {
                    Toast.makeText(LoginActivity.this,
                            getResources().getString(R.string.pwd_cannot_empty),
                            Toast.LENGTH_SHORT).show();
                } else {
                    User user = new User(usernameEdit.getText().toString().trim(),
                            pwdEdit.getText().toString().trim());
                    userService.addUser(user)
                            .subscribeOn(Schedulers.newThread())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Subscriber<Result<Integer>>() {
                                @Override
                                public void onCompleted() {

                                }

                                @Override
                                public void onError(Throwable e) {
                                    Logger.e(e, e.getMessage());

                                }

                                @Override
                                public void onNext(Result<Integer> result) {
                                    if (result.getCode().equals(Result.SUCCESS)) {
                                        Toast.makeText(LoginActivity.this,
                                                getResources().getString(R.string.register_success),
                                                Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(LoginActivity.this,
                                                result.getMessage(), Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                }
                break;
            case R.id.login_btn:
                if (usernameEdit.getText().toString().trim().isEmpty()) {
                    Toast.makeText(LoginActivity.this,
                            getResources().getString(R.string.user_name_cannot_empty),
                            Toast.LENGTH_SHORT).show();
                } else if (pwdEdit.getText().toString().trim().isEmpty()) {
                    Toast.makeText(LoginActivity.this,
                            getResources().getString(R.string.pwd_cannot_empty),
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LoginActivity.this, "loading", Toast.LENGTH_SHORT).show();
                    User user = new User(usernameEdit.getText().toString().trim(),
                            pwdEdit.getText().toString().trim());
                    userService.login(user)
                            .subscribeOn(Schedulers.newThread())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Subscriber<Result<Integer>>() {
                                @Override
                                public void onCompleted() {

                                }

                                @Override
                                public void onError(Throwable e) {
                                    Logger.e(e, e.getMessage());
                                }

                                @Override
                                public void onNext(Result<Integer> result) {
                                    if (result.getCode().equals(Result.SUCCESS)) {
                                        user.setId(result.getBean());
                                        AppController.getInstance().getCache().put("user",
                                                JSON.toJSONString(user));
                                        toMainActivity();
                                    } else {
                                        Toast.makeText(LoginActivity.this,
                                               getResources().getString(R.string.username_pwd_wrong),
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                }
                break;
        }
    }

    private void toMainActivity() {
        Logger.d("to main activity");
        Intent toMain = new Intent(LoginActivity.this,
                MainActivity.class);
        startActivity(toMain);
        finish();
    }
}
